package com.ft.oa.web.approval;

import com.ft.oa.domain.approval.ApplyForApproval;
import com.ft.oa.domain.dto.ApiResult;
import com.ft.oa.service.approval.ApplyForApprovalService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 待办中心--审批相关
 * @author liyansheng
 * @ClassName ApplyForApprovalController
 * @date 2022-1-18 17:36:30
 */
@RestController
@RequestMapping("/approval")
public class ApplyForApprovalController {

    @Resource
    private ApplyForApprovalService applyForApprovalService;

    /**
     * 根据根据用户查询代办（待审批）数据、已审批数据、抄送数据
     * 关联业务表主键 toDoList-待办 doneList-已办 copyList-抄送
     * @param
     * @return
     */
    @PostMapping("/todoList")
    public ApiResult selectToDoList() {
        //根据登录人进行查询
        Long userId = SecurityUtils.getCurrentUser().getId();
        return applyForApprovalService.selectToDoList(userId);
    }

    /**
     * 审批通过
     * @param applyForApproval
     * @return
     */
    @PostMapping("/approve")
    public ApiResult approve(@RequestBody ApplyForApproval applyForApproval) {
        return applyForApprovalService.approve(applyForApproval);
    }

    /**
     * 驳回
     * @param applyForApproval
     * @return
     */
    @PostMapping("/reject")
    public ApiResult reject(@RequestBody ApplyForApproval applyForApproval) {
        return applyForApprovalService.reject(applyForApproval);
    }

    /**
     * 抄送人提意见 一个抄送人对同一申请可以提多条意见（目前设计是在每个意见基础上加上<br />换行符,需要前端去解析标签）
     * id（抄送表主键） 和 approvalOpinion（审批意见） 为必录项
     * @param applyForApproval
     * @return
     */
    @PostMapping("/exchangeViews")
    public ApiResult exchangeViews(@RequestBody ApplyForApproval applyForApproval) {
        return applyForApprovalService.exchangeViews(applyForApproval);
    }

    /**
     * 根据业务编码查询所有审批人抄送人
     * @param relationId  关联业务表主键
     * @param relationType 关联审批分类 1、合同审批 2、协同审批 3、投标审批  4、立项审批  5、验收审批
     *       6、成本变更审批  7、进度变更审批  8、研发立项审批  9、采购审批
     * @return
     */
    @PostMapping("/selectAllUser/{relationType}/{relationId}")
    public ApiResult selectAllUser(@PathVariable("relationId")  Long relationId,@PathVariable("relationType") Integer relationType) {
        return applyForApprovalService.selectAllUser(relationId,relationType);
    }

    /**
     * 根据业务编码查询所有审批人
     * @param relationId  关联业务表主键
     * @param relationType 关联审批分类 1、合同审批 2、协同审批 3、投标审批  4、立项审批  5、验收审批
     *       6、成本变更审批  7、进度变更审批  8、研发立项审批  9、采购审批
     * @return
     */
    @PostMapping("/selectApprovalUser/{relationType}/{relationId}")
    public ApiResult selectApprovalUser(@PathVariable("relationId")  Long relationId,@PathVariable("relationType") Integer relationType) {
        List<ApplyForApproval> applyForApprovals = applyForApprovalService.selectApprovalUser(relationId,relationType);
        return ApiResult.successWithData(applyForApprovals);
    }

    /**
     * 根据业务编码查询所有抄送人
     * @param relationId  关联业务表主键
     * @param relationType 关联审批分类 1、合同审批 2、协同审批 3、投标审批  4、立项审批  5、验收审批
     *       6、成本变更审批  7、进度变更审批  8、研发立项审批  9、采购审批
     * @return
     */
    @PostMapping("/selectCopyUser/{relationType}/{relationId}")
    public ApiResult selectCopyUser(@PathVariable("relationId")  Long relationId,@PathVariable("relationType") Integer relationType) {
        List<ApplyForApproval> applyForApprovals = applyForApprovalService.selectCopyUser(relationId,relationType);
        return ApiResult.successWithData(applyForApprovals);
    }

    /**
     * 增加审批人
     * @param applyForApproval
     * @return
     */
    @PostMapping("/addApproval")
    public ApiResult addApproval(@RequestBody List<ApplyForApproval> applyForApproval){
        ApiResult result = applyForApprovalService.addApproval(applyForApproval);
        return result;
    }

    /**
     * 增加抄送人
     * @param applyForApproval
     * @return
     */
    @PostMapping("/addCarbonCopy")
    public ApiResult addCarbonCopy(@RequestBody List<ApplyForApproval> applyForApproval){
        ApiResult result = applyForApprovalService.addCarbonCopy(applyForApproval);
        return result;
    }

    /**
     * 已读
     * @return
     */
    @PostMapping("/read/{id}")
    public ApiResult read(@PathVariable("id")  Integer id) {
        return applyForApprovalService.read(id);
    }


}
